<html>
<head><meta charset="utf-8"><title>Fn traits -&gt; fn pointer · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html">Fn traits -&gt; fn pointer</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="179693357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693357">(Nov 02 2019 at 08:45)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> <span class="user-mention" data-user-id="124288">@oli</span> I was trying to answer to <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/218" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/218">ucg#218</a> and came up with this code which fails under miri (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=b69833b35c30301975231b942b605be3" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=b69833b35c30301975231b942b605be3">playground</a>): </p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">meaning_of_life</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"> </span><span class="mi">42</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">union</span> <span class="nc">U</span><span class="o">&lt;</span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="o">+</span><span class="w"> </span><span class="nb">Copy</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">F</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">}</span><span class="w"></span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">a</span><span class="o">&lt;</span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="o">+</span><span class="w"> </span><span class="nb">Copy</span><span class="o">&gt;</span><span class="p">(</span><span class="n">t</span>: <span class="nc">F</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">fn</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">U</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">t</span><span class="w"> </span><span class="p">}.</span><span class="n">y</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">meaning_of_life</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</pre></div>



<a name="179693364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693364">(Nov 02 2019 at 08:45)</a>:</h4>
<p>While I see why it might fail, I'm not sure I agree that this should fail</p>



<a name="179693417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693417">(Nov 02 2019 at 08:47)</a>:</h4>
<p>The actual type of <code>F</code> in <code>a(meaning_of_life)</code> is <code>F: fn() -&gt; i32 {meaning_of_life}</code></p>



<a name="179693418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693418">(Nov 02 2019 at 08:47)</a>:</h4>
<p>and transmuting that to a <code>fn() -&gt; i32</code> is ok</p>



<a name="179693472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693472">(Nov 02 2019 at 08:48)</a>:</h4>
<p>The error I get is:</p>
<div class="codehilite"><pre><span></span>U { x: t }.y
^^^^^^^^^^^^ Miri evaluation error: type validation failed: encountered uninitialized bytes, but expected something greater or equal to 1
note: inside call to `a::&lt;fn() -&gt; i32 {meaning_of_life}&gt;`
</pre></div>



<a name="179693483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693483">(Nov 02 2019 at 08:49)</a>:</h4>
<p>And I'm not sure how that is happening, since the type of <code>F</code> is correct, and therefore the transmute to <code>fn()-&gt;i32</code> looks correct to me as well</p>



<a name="179693735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693735">(Nov 02 2019 at 08:56)</a>:</h4>
<p>(and well notice that Rust segfaults, so, something weird is going on)</p>



<a name="179693743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693743">(Nov 02 2019 at 08:57)</a>:</h4>
<p><code>F</code> is a ZST, you can't <em>transmute</em> that to a fn pointer, you can only <em>cast</em> (or coerce) it which boils down to taking the address of the function rather than mere type punning</p>



<a name="179693757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693757">(Nov 02 2019 at 08:57)</a>:</h4>
<p>so that explains the uninitialized bytes</p>



<a name="179693807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693807">(Nov 02 2019 at 08:58)</a>:</h4>
<p>I still find it weird that <code>U { x: fn() -&gt; i32 {foo}, y: fn() -&gt; i32 }</code> is a "useless" union</p>



<a name="179693825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693825">(Nov 02 2019 at 08:59)</a>:</h4>
<p>but yeah, that can't work</p>



<a name="179693878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693878">(Nov 02 2019 at 09:00)</a>:</h4>
<p>We could maybe add a <code>Fn::as_fn(self) -&gt; Option&lt;fn(Args) -&gt; Ret&gt;</code> method to the function traits</p>



<a name="179693892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693892">(Nov 02 2019 at 09:01)</a>:</h4>
<p>that returns <code>Some</code> if the type can be coerced into a function pointer of the appropriate type or similar</p>



<a name="179693977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179693977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179693977">(Nov 02 2019 at 09:03)</a>:</h4>
<p>or somehow support <code>as</code> expressions for the function traits</p>



<a name="179694041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179694041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179694041">(Nov 02 2019 at 09:05)</a>:</h4>
<p>Is there a scenario where you need to do that conversion <em>after</em> abstracting the concrete type to a <code>F. Fn</code>? <em>And</em> can deal with it possibly failing? In most circumstances, I imagine you could just as well do the <code>as</code> cast before forgetting the concrete type that can be cast to a fn pointer type.</p>



<a name="179694177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179694177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179694177">(Nov 02 2019 at 09:09)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> I don't know, but if you want to cast a <code>Fn</code> to a <code>fn</code> in a generic context, that cast can fail.</p>



<a name="179694183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179694183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179694183">(Nov 02 2019 at 09:09)</a>:</h4>
<p>Obviously. So why try to do that, unless something else forces your hand?</p>



<a name="179694225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179694225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179694225">(Nov 02 2019 at 09:10)</a>:</h4>
<p>No idea, I've never needed this, but I'll ask the user.</p>



<a name="179694296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179694296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179694296">(Nov 02 2019 at 09:12)</a>:</h4>
<blockquote>
<p>And can deal with it possibly failing?</p>
</blockquote>
<p>You can actually deal with this, by creating a function that uses the <code>Fn</code> via some external context</p>



<a name="179694304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/179694304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#179694304">(Nov 02 2019 at 09:13)</a>:</h4>
<p>(e.g. a static, thread local, etc. but........... sounds like a bad idea anyways)</p>



<a name="180366991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn%20traits%20-%3E%20fn%20pointer/near/180366991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Fn.20traits.20-.3E.20fn.20pointer.html#180366991">(Nov 10 2019 at 15:06)</a>:</h4>
<p>Maybe related, I think it would be useful to have a trait shared by function "items" and function items only:</p>
<div class="codehilite"><pre><span></span><span class="cp">#[waving_hands]</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">FnItem</span><span class="o">&lt;</span><span class="n">Args</span><span class="o">&gt;</span><span class="w"> </span>: <span class="nb">Copy</span> <span class="o">+</span><span class="w"> </span><span class="nb">Sync</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">Frozen</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Fn</span><span class="o">&lt;</span><span class="n">Args</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;static</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">call_item</span><span class="w"> </span><span class="p">(</span><span class="n">args</span>: <span class="nc">Args</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">&lt;</span><span class="n">Self</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">Fn</span><span class="o">&lt;</span><span class="n">Args</span><span class="o">&gt;&gt;</span>::<span class="n">Output</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">CALL_POINTER</span>: <span class="nc">fn</span><span class="p">(</span><span class="n">Args</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="o">&lt;</span><span class="n">Self</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">Fn</span><span class="o">&lt;</span><span class="n">Args</span><span class="o">&gt;&gt;</span>::<span class="n">Output</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>And then with <code>F : FnItem(...) -&gt; _</code> one could get a guaranteed function "item" with <code>F::call_item</code>, and lifetime-monomorphized <code>fn</code> pointer with the constant.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>